package com.duobaoyu.dby.supply.biz.strategy;

import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.supply.biz.converter.PreWarningConfigConverter;
import com.duobaoyu.dby.supply.common.biz.dto.config.PreWarningConditionDTO;
import com.duobaoyu.dby.supply.core.framework.strategy.AbstractStrategy;
import com.duobaoyu.dby.supply.db.entity.config.PreWarningConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;
import java.util.Set;

/**
 * @author chenms
 * @date 2023/8/16 2:50 PM
 */
@Slf4j
public abstract class AbstractPreWarningCountStrategy extends AbstractStrategy<PreWarningConfig,Long> {

	@Autowired
	private PreWarningConfigConverter preWarningConfigConverter;
    /**
     * 查询预配置数量
     * @return
     */
    protected abstract long count(PreWarningConditionDTO preWarningCondition);

    /**
     * 支持的预警类型
     * @return
     */
    protected abstract Set<Integer> supportPreWarningTypes();

    @Override
    public boolean support(Object... objects) {
        Set<Integer> types = supportPreWarningTypes();
        if(CollectionUtils.isEmpty(types)) {
            return false;
        }
        if(objects.length != CommonConstants.ONE.intValue()) {
            return false;
        }
        return types.contains(objects[CommonConstants.ZERO]);
    }

    @Override
    protected Long handler(PreWarningConfig preWarningConfig, Object beforeResult) {
        PreWarningConditionDTO preWarningCondition = preWarningConfigConverter.parsePreWarningCondition(preWarningConfig);
        return Objects.isNull(preWarningConfig) ? CommonConstants.ZERO.longValue() : this.count(preWarningCondition);
    }
}
